<?php
/**
 * Created by PhpStorm.
 * User: kaycn
 * Date: 18-9-13
 * Time: 下午10:48
 */
namespace Pay;

use Facebook\WebDriver\WebDriverBy;

class Screen{

    public $vcodeDst = "vcode.png";

    public $driver;


    public function __construct($driver)
    {
        $this->driver = $driver;


    }

    public function frame(){

        $this->driver->manage()->window()->maximize();    //将浏览器最大化

        $this->driver->takeScreenshot($this->vcodeDst);

        $this->driver ->switchTo()->window("");

        $element = $this->driver->findElement(WebDriverBy::id('J_loginIframe'));

        $this->generateVcodeIMG($element->getLocation(), $element->getSize(),$this->vcodeDst);

        $this->driver -> switchTo()->frame("J_loginIframe");

        $element = $this->driver->findElement(WebDriverBy::id('J-checkcode-img'));

        sleep(1);

        $this->generateVcodeIMG($element->getLocation(), $element->getSize(),$this->vcodeDst);


    }

    private function generateVcodeIMG($location,$size,$src_img){
        $width = $size->getWidth();
        $height = $size->getHeight();
        $x = $location->getX();
        $y = $location->getY();

        echo $width."\n";
        echo $height."\n";
        echo $x."\n";
        echo $y."\n";

        $src = imagecreatefrompng($src_img);
        $dst = imagecreatetruecolor($width,$height);
        imagecopyresampled($dst,$src,0,0,$x,$y,$width,$height,$width,$height);
        imagepng($dst,$src_img);
        chmod($src_img,0777);
        imagedestroy($src);
        imagedestroy($dst);
    }

}